﻿function JEEmailDialog(selector) {

    var that = this;

    var _selector = selector;

    var _auditTrailComments = null;

    that.ccClick = function () {
        var arr = $('#EmailCCList').val().split(',');
        if (arr == '') {
            arr.shift();
        }
        $('#userList').find('option:selected').each(function () {
            if (jQuery.inArray($(this).val(), arr) == -1) {
                arr.push($(this).val());
            }
        });
        $('#EmailCCList').val(arr.join(','));
    };

    that.emailPreparerCheckbox = function (e) {
        var valChk = $('#EmailPreparerCheckbox').is(":checked");
        if (valChk) {
            $('#EmailSubject').attr("disabled", false);
            $('#EmailComments').attr("disabled", false);
        }
        else {
            var isEmailOthersChk = $('#EmailOthersCheckbox').is(":checked");
            if (!isEmailOthersChk) {
                $('#EmailSubject').attr("disabled", true);
            }
        }
    };

    that.emailOthersCheckbox = function (e) {
        var valChk = $('#EmailOthersCheckbox').is(":checked");
        if (valChk) {
            $('#ButtonCC').attr("disabled", false);
            $('#EmailToList').attr("disabled", false);
            $('#ButtonCC').attr("disabled", false);
            $('#ButtonTo').attr("disabled", false);
            $('#EmailCCList').attr("disabled", false);
            $('#userList').attr("disabled", false);
            $('#EmailSubject').attr("disabled", false);
            $('#EmailComments').attr("disabled", false);
        }
        else {
            $('#ButtonCC').attr("disabled", true);
            $('#EmailToList').attr("disabled", true);
            $('#ButtonCC').attr("disabled", true);
            $('#ButtonTo').attr("disabled", true);
            $('#EmailCCList').attr("disabled", true);
            $('#userList').attr("disabled", true);
            var isEmailPrepChk = $('#EmailPreparerCheckbox').is(":checked");
            if (!isEmailPrepChk) {
                $('#EmailSubject').attr("disabled", true);
            }
        }
    };


    that.toClick = function (e) {
        var arr = $('#EmailToList').val().split(',');
        if (arr == '') {
            arr.shift();
        }
        $('#userList').find('option:selected').each(function () {
            if (jQuery.inArray($(this).val(), arr) == -1) {
                arr.push($(this).val());
            }
        });
        $('#EmailToList').val(arr.join(','));

        console.log("toClick");
    };

    that.submit = function () {
        setAuditTrailComments();
        var formData = $('#JEEmailForm').serialize();
        if ($('#EmailOthersCheckbox').is(":checked") && !$('#EmailPreparerCheckbox').is(":checked")) {
            if($('#JEEmailForm').valid()) {
                _callback.submit(formData);
            }
        } else {
            _callback.submit(formData);
        }
    };

    var _callback;

    that.shouldSendEmail = function () {
        return $('#EmailOthersCheckbox').is(":checked");
    };

    that.open = function (result, callback, title) {
        _callback = callback;
        _selector.html(result);
        _selector.dialog('option', 'title', title);
        _selector.dialog('open');
        $('#JEEmailForm').submit(function (e) {
            e.preventDefault();
        });
        $('#EmailPreparerCheckbox').click(that.emailPreparerCheckbox);
        $('#EmailOthersCheckbox').click(that.emailOthersCheckbox);
        $('#ButtonTo').click(that.toClick);
        $('#ButtonCC').click(that.ccClick);
        $('#EmailModalSubmitButton').click(that.submit);
        $('#EmailModalCancelButton').click(_callback.cancel);
    };

    that.closeModal = function() {
        _selector.dialog('close');
    };

    that.setupEmailFormDialog = function (element) {
        element.dialog(
            {
                autoOpen: false,
                modal: true,
                resizable: false,
                width: 900,
                minHeight: '200px',
                zIndex: 10000,
                open: function () {
                    // recenter dialog on open
                    $(this).dialog('widget').position({ my: "center", at: "center", of: window });
                }
            }
        );
    };

    that.getAuditTrailComments = function () {
        return _auditTrailComments;
    };

    function setAuditTrailComments() {
        var auditTrailCommentsCheckbox = $('#AuditTrailCommentsCheckbox').is(':checked');
        var comments = $('#EmailComments').val();
        if (auditTrailCommentsCheckbox && comments) {
            _auditTrailComments = comments;
        }
    }

    that.setupEmailFormDialog(_selector);

    return that;
}